<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" top="30px" destroy-on-close
             @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="80px"
             label-position="right">

      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="form.nickName" placeholder="请输入用户昵称" maxlength="10" show-word-limit clearable></el-input>
      </el-form-item>
      <el-form-item label="登录账号" prop="userName" >
        <el-input :is-editing="editShow" :disabled="editShow" v-model="form.userName" placeholder="请输入登录账号" maxlength="20"
                  onkeyup="value=value.replace(/[^\x00-\xff]/g, '')" show-word-limit clearable>
        </el-input>
      </el-form-item>
      <template v-if="mode == 'add'">
        <el-form-item label="登录密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" maxlength="20" minlength="8"
                    clearable show-password></el-input>
          <sc-password-strength v-model="form.password"></sc-password-strength>
          <div class="el-form-item-msg">建议输入至少8位包含字母、数字、特殊字符的组合字符串</div>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input type="password" v-model="form.password2" placeholder="请再次输入密码" maxlength="20"
                    minlength="8" clearable show-password></el-input>
        </el-form-item>
      </template>
      <!-- <el-form-item label="所属角色" prop="roleIds">
        <el-select v-model="form.roleIds" placeholder="请选择所属角色" multiple filterable style="width: 100%">
          <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import scPasswordStrength from '@/components/scPasswordStrength'

export default {
  components: {
    scPasswordStrength
  },
  emits: ['success', 'closed'],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: '新增用户',
        edit: '编辑用户',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      editShow:false,
      //表单数据
      form: {
        id: "",
        userName: "",
        avatar: "",
        nickName: "",
        deptId: null,
        bizCode: "",
        status: '1',
        type: '1',
        remark: ""
      },
      //验证规则
      rules: {
        userName: [
          { required: true, message: '登录账号不能为空' },
          {
            required: true,
            validator: (rule, value, callback) => {
              var params = {userName: value, userId: this.form.id}
              this.$API.system.user.validateUserName.get(params).then(res => {
                if (res.data >= 1) {
                  return callback(new Error('此账号 ' + value + ' 已存在, 请修改'))
                }
                callback()
              })
            },
            trigger: 'blur'
          }
        ],
        nickName: [
          { required: true, message: '用户昵称不能为空' }
        ],
        password: [
          { required: true, message: '登录密码不能为空' },
          {
            validator: (rule, value, callback) => {
              if (value.length < 6) {
                return callback(new Error('密码长度不能小于6位'))
              }
              callback();
            }
          }
        ],
        password2: [
          { required: true, message: '请再次输入密码' },
          {
            validator: (rule, value, callback) => {
              if (value !== this.form.password) {
                callback(new Error('两次输入密码不一致'));
              } else {
                callback();
              }
            }
          },
          {
            validator: (rule, value, callback) => {
              if (value.length < 6) {
                return callback(new Error('密码长度不能小于6位'))
              }
              callback();
            }
          }
        ]
      },
    }
  },
  mounted() {
  },
  methods: {
    //显示
    open(mode = 'add') {
      this.mode = mode;
      this.visible = true;
      return this
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res;
          this.form.password = this.$TOOL.crypto.MD5(this.form.password)
          if (this.mode == 'add') {
            res = await this.$API.system.user.add.post(this.form)
          } else if (this.mode == 'edit') {
            delete this.form.password
            res = await this.$API.system.user.update.put(this.form)
          }
          this.isSaveing = false;
          if (res.code == 200) {
            this.visible = false;
            this.$emit('success', this.form, this.mode)
            this.$message.success("操作成功")
          } else {
            this.$alert(res.msg, "提示", { type: 'error' })
          }
        } else {
          return false;
        }
      })
    },
    //表单注入数据
    setData(row) {
      Object.assign(this.form, row)
      if(this.mode=='edit'){
        this.editShow=true;
      }else {
        this.editShow=false;
      }
    }
  }
}
</script>

<style>

</style>
